/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TicketGui.java
 *
 * Created on 21 févr. 2012, 20:58:32
 */
package app.views;

import app.Tools;
import app.models.Factory;
import app.models.Lignevente;
import app.models.Vente;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import javax.persistence.*;
import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;

/**
 *
 * @author Ghost
 */
public class TicketGui extends javax.swing.JPanel implements Printable {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");

    /** Creates new form TicketGui */
    public TicketGui(Vente v) {
        initComponents();
        displayVente(v);
    }

    public void displayVente(Vente v) {

        StringBuilder sb = new StringBuilder();
        sb.append("===========================\n");
        sb.append("   ____ _______ _____     \n"
                + "  |  _ \\__   __|  __ \\  \n"
                + "  | |_) | | |  | |__) |   \n"
                + "  |  _ <  | |  |  _  /    \n"
                + "  | |_) | | |  | | \\ \\  \n"
                + "  |____/  |_|  |_|  \\_\\ \n"
                + "\n");
        sb.append("===========================\n");
        sb.append(" Vente no: ").append(v.getId());
        sb.append("\n le ").append(sdf.format(v.getDateV()));
        sb.append("\n===========================\n");
        for (Lignevente lv : v.getLigneventeList()) {
            sb.append(lv.getReference().getDesignation()).append("\n");
            String strQte = " " + lv.getQuantite() + "x";
            String strPrice = lv.getPxVente() + " eur.";
            sb.append(strQte);
            int nbWhiteSpace = 27 - strQte.length() - strPrice.length();
            for (int i = 0; i < nbWhiteSpace; i++) {
                sb.append(" ");
            }
            sb.append(strPrice).append("\n");
        }

        sb.append("\n===========================\n");
        String strLib = " Total:";
        String strPrice = v.getTotal() + "eur.";

        int nbWhiteSpace = 27 - strLib.length() - strPrice.length();
        sb.append(strLib);
        for (int i = 0; i < nbWhiteSpace; i++) {
            sb.append(" ");
        }
        sb.append(strPrice);



        txtPrint.setText(sb.toString());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtPrint = new javax.swing.JTextArea();
        btPrint = new javax.swing.JButton();
        btClose = new javax.swing.JButton();

        txtPrint.setColumns(25);
        txtPrint.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
        txtPrint.setRows(10);
        txtPrint.setText(" ____ _______ _____  \n|  _ \\__   __|  __ \\ \n| |_) | | |  | |__) |\n|  _ <  | |  |  _  / \n| |_) | | |  | | \\ \\ \n|____/  |_|  |_|  \\_\\");
            txtPrint.setDisabledTextColor(new java.awt.Color(0, 0, 0));
            txtPrint.setEnabled(false);
            txtPrint.setPreferredSize(new java.awt.Dimension(250, 250));
            jScrollPane1.setViewportView(txtPrint);

            btPrint.setText("Imprimer");
            btPrint.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btPrintActionPerformed(evt);
                }
            });

            btClose.setText("Fermer");

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btClose)
                            .addGap(18, 18, 18)
                            .addComponent(btPrint)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btPrint)
                        .addComponent(btClose))
                    .addContainerGap())
            );
        }// </editor-fold>//GEN-END:initComponents

    private void btPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrintActionPerformed
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(this);
        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (PrinterException pe) {
                System.out.println("Error printing: " + pe);
            }
        }
    }//GEN-LAST:event_btPrintActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btClose;
    private javax.swing.JButton btPrint;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtPrint;
    // End of variables declaration//GEN-END:variables

    /*public static void main(String... args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            Tools.debug("LAF Error 2:" + e);
        }

        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        Query q = Factory.getContext().createNamedQuery("Vente.findById").setParameter("id", 11);
        Vente v = (Vente) q.getSingleResult();
        f.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent e) {
                System.out.println(e.getComponent().getSize());
            }
        });

        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.add(new TicketGui(v));
        f.pack();
        f.setVisible(true);
    }*/

    @Override
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
        // We have only one page, and 'page'
        // is zero-based
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        // User (0,0) is typically outside the
        // imageable area, so we must translate
        // by the X and Y values in the PageFormat
        // to avoid clipping.
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pageFormat.getImageableX(),
                pageFormat.getImageableY());

        txtPrint.printAll(g);

        // tell the caller that this page is part
        // of the printed document
        return PAGE_EXISTS;
    }
}
